﻿<div id="gridUserOrgPositions"></div>
<div id="dlgAddUserOrgPosition" style="display:none;padding-left:60px;">
    <form id="formAddUserOrgPosition"></form>
</div>

<script type="text/javascript">
    var gridUserOrgPositions = null;
    var dlgAddUserOrgPosition = null;
    var formAddUserOrgPosition = null;

    function loadCombox(_name, _url) {
        var cbo = liger.get(_name);
        cbo.setUrl(_url);
        cbo.reload();
    }
</script>

<script type="text/javascript">
    function addUserOrgPosition() {
        $.post('/API/UserOrgPosition/Create', formAddUserOrgPosition.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('添加成功', '提示');
                loadUserOrgPositions();
                dlgAddUserOrgPosition.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function showDlgAddUserOrgPosition() {
        if (dlgAddUserOrgPosition) {
            dlgAddUserOrgPosition.show();
        }
        else {
            dlgAddUserOrgPosition = $.ligerDialog.open({
                target: $('#dlgAddUserOrgPosition'),
                width: 440,
                height: 240,
                title: '添加新用户',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formAddUserOrgPosition.valid()) {

                                addUserOrgPosition();
                            }
                            else {
                                formAddUserOrgPosition.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgAddUserOrgPosition.hide();
                        }
                    }
                ]
            });
        }
        
        loadCombox('cboPosition', '/API/Position/Query?type=combox');
        liger.get('cboOrg').reload();
        loadCombox('cboUser', '/API/User/GetUsers');        
        loadCombox('cboParentUser', '/API/User/GetUsers');
    }

    function initForm() {
        formAddUserOrgPosition = $('#formAddUserOrgPosition').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { name: 'UserId', type: 'hidden' },
                {
                    display: '姓名', name: "UserId", newline: true, type: "select",
                    comboboxName: 'cboUser', validate: { required: true },
                    options: {
                        textField: 'UserName', valueField: 'Id'
                    }
                },
                {
                    display: "部门", name: "OrgId", comboboxName: 'cboOrg', newline: true,
                    type: "select", validate: { required: true },
                    editor: {
                        selectBoxWidth: 600,
                        selectBoxHeight: 300,
                        textField: 'OrgName',
                        valueField: 'Id',
                        grid: {
                            columns: [{ display: '部门名', name: 'OrgName', align: 'left', width: 200, minwidth: 120 }],
                            rownumbers: true,
                            usePager: false,
                            tree: {
                                columnName: 'OrgName',
                                idField: 'Id',
                                parentIDField: 'ParentOrgId'
                            },
                            url: '/API/Org/Query'
                        }
                    }
                },
                {
                    display: '职位', name: "PositionId", newline: true, type: "select",
                    comboboxName: 'cboPosition', validate: { required: true },
                    options: {
                        textField: 'PositionName', valueField: 'Id'
                    }
                },
                {
                    display: '直属上级', name: "ParentUserId", newline: true, type: "select",
                    comboboxName: 'cboParentUser',
                    options: {
                        textField: 'UserName', valueField: 'Id',
                        url: '/API/User/GetUsers'
                    }
                }
            ],
            validate: {}
        });
    }
</script>

<script type="text/javascript">
    function deleteUserOrgPosition(row) {
        $.post('/API/UserOrgPosition/Delete', row,
            function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('删除成功', '提示');
                    loadUserOrgPositions();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
    }

    function itemclick(item) {
        switch (item.method) {
            case 'add':
                showDlgAddUserOrgPosition();
                break;

            case 'delete':
                DoActionConfirm(gridUserOrgPositions, deleteUserOrgPosition, '请选择需要修删除的用户', '是否要删除当前选择的用户？');
                break;
        }
    }

    var toolbarUserOrgPositions = {
        items: [
            { method: 'add', text: '增加', click: itemclick, icon: 'add' },
            { line: true },
            { method: 'delete', text: '删除', click: itemclick, icon: 'delete' }
        ]
    };

    var columnUserOrgPositions = [
        { display: '姓名', name: 'UserName', align: 'left', width: 150, minwidth: 120 },
        { display: '部门', name: 'Org', align: 'left', width: 150, minwidth: 120 },
        { display: '职位', name: 'Position', align: 'left', width: 120, minwidth: 80 },
        { display: '直属上级', name: 'ParentUserName', align: 'left', width: 180, minwidth: 120 }
    ];

    function loadUserOrgPositions() {
        var complete = false;
        $.post('/API/UserOrgPosition/Query', {}, function (data) {
            complete = true;
            gridUserOrgPositions.loadData(data);
            gridUserOrgPositions.gridloading.hide(100);
        }, 'json');

        var n = setInterval(function () {
            clearInterval(n);
            if (!complete) {
                gridUserOrgPositions.gridloading.show();
            }
        }, 100);
    }

    $(document).ready(function () {
        $.metadata.setType("attr", "validate");
        initForm();
        gridUserOrgPositions = $('#gridUserOrgPositions').ligerGrid({
            title: '用户职位信息',
            columns: columnUserOrgPositions,
            toolbar: toolbarUserOrgPositions,
            rownumbers: true,
            resizable: false,
            frozen: false,
            dataAction: 'local'
        });

        loadUserOrgPositions();
    });
</script>